<template>
  <el-dialog
    :title="!dataForm.userBehaviorWeightId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="点赞的权重" prop="likeWeight">
      <el-input v-model="dataForm.likeWeight" placeholder="点赞的权重"></el-input>
    </el-form-item>
    <el-form-item label="收藏的权重" prop="collectionWeight">
      <el-input v-model="dataForm.collectionWeight" placeholder="收藏的权重"></el-input>
    </el-form-item>
    <el-form-item label="评论的权重" prop="commentWeight">
      <el-input v-model="dataForm.commentWeight" placeholder="评论的权重"></el-input>
    </el-form-item>
    <el-form-item label="点击次数的系数" prop="clicksWeight">
      <el-input v-model="dataForm.clicksWeight" placeholder="点击次数的系数"></el-input>
    </el-form-item>
    <el-form-item label="1为当前采用的配置，0不是当前采用的配置" prop="nowConfig">
      <el-input v-model="dataForm.nowConfig" placeholder="1为当前采用的配置，0不是当前采用的配置"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userBehaviorWeightId: 0,
          likeWeight: '',
          collectionWeight: '',
          commentWeight: '',
          clicksWeight: '',
          nowConfig: ''
        },
        dataRule: {
          likeWeight: [
            { required: true, message: '点赞的权重不能为空', trigger: 'blur' }
          ],
          collectionWeight: [
            { required: true, message: '收藏的权重不能为空', trigger: 'blur' }
          ],
          commentWeight: [
            { required: true, message: '评论的权重不能为空', trigger: 'blur' }
          ],
          clicksWeight: [
            { required: true, message: '点击次数的系数不能为空', trigger: 'blur' }
          ],
          nowConfig: [
            { required: true, message: '1为当前采用的配置，0不是当前采用的配置不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userBehaviorWeightId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userBehaviorWeightId) {
            this.$http({
              url: this.$http.adornUrl(`/recommend/userbehaviorweight/info/${this.dataForm.userBehaviorWeightId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.likeWeight = data.userBehaviorWeight.likeWeight
                this.dataForm.collectionWeight = data.userBehaviorWeight.collectionWeight
                this.dataForm.commentWeight = data.userBehaviorWeight.commentWeight
                this.dataForm.clicksWeight = data.userBehaviorWeight.clicksWeight
                this.dataForm.nowConfig = data.userBehaviorWeight.nowConfig
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/recommend/userbehaviorweight/${!this.dataForm.userBehaviorWeightId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userBehaviorWeightId': this.dataForm.userBehaviorWeightId || undefined,
                'likeWeight': this.dataForm.likeWeight,
                'collectionWeight': this.dataForm.collectionWeight,
                'commentWeight': this.dataForm.commentWeight,
                'clicksWeight': this.dataForm.clicksWeight,
                'nowConfig': this.dataForm.nowConfig
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
